package tools;

import java.util.Random;

public class Utils {

    public static byte[] concat(byte[] b1, byte[] b2) {
        byte[] b3 = new byte[b1.length + b2.length];
        System.arraycopy(b1, 0, b3, 0, b1.length);
        System.arraycopy(b2, 0, b3, b1.length, b2.length);
        return b3;
    }
    
    public static void setBit(byte[] data, int pos, int val) {
        int posByte = pos / 8;
        int posBit = pos % 8;
        byte oldByte = data[posByte];
        oldByte = (byte) (((0xFF7F >> posBit) & oldByte) & 0x00FF);
        byte newByte = (byte) ((val << (8 - (posBit + 1))) | oldByte);
        data[posByte] = newByte;
    }

    public static int getBit(byte[] data, int pos) {
        int posByte = pos / 8;
        int posBit = pos % 8;
        byte valByte = data[posByte];
        int valInt = valByte >> (8 - (posBit + 1)) & 0x0001;
        return valInt;
    }

    public static byte[] generatePeerId() {

        byte[] peer_id = new byte[20];
        byte[] version = Parametres.version.getBytes();
        Random random = new Random();

        for (int i = 0; i < version.length; i++) {
            peer_id[i] = version[i];
        }

        String chars = "0123456789";

        for (int i = version.length; i < 20; i++) {
            char carac = chars.charAt(random.nextInt(10));
            peer_id[i] = ((byte) ((int) carac));
        }
        
        System.out.println("peer_id="+new String(peer_id));
        return peer_id;

    }

   

    
}
